<template>
    <view class="context">
        <saleTitle></saleTitle>
        <saleMenu ref="saleMenu" @loadData="loadData"></saleMenu>
        <view class="box">
            <view class="s-h-box">
                <saleHead ref="saleHead"></saleHead>
            </view>
            <view class="s-c-t-f-box">
                <salesComp ref="salesComp"></salesComp>
                <saleRanking
                    ref="saleRanking"
                    @saleBunsinessIncome="saleBunsinessIncome"
                ></saleRanking>
                <saleFlows
                    ref="saleFlows"
                    @loadFlowsList="loadFlowsList"
                    @saleBunsinessIncome="saleBunsinessIncome"
                ></saleFlows>
            </view>
        </view>
        <!-- 季度流水 -->
        <saleQuarterWin
            ref="saleQuarterWin"
            @saleBunsinessIncome="saleBunsinessIncome"
        ></saleQuarterWin>
        <!-- 月流水 -->
        <saleMoothWin
            ref="saleMoothWin"
            @saleBunsinessIncome="saleBunsinessIncome"
        ></saleMoothWin>
        <!-- 日销售流水 -->
        <saleBusIncomeWin
            ref="saleBusIncomeWin"
            @saleBunsinessIncome="saleBunsinessIncome"
            @loadFun="loadFun"
        ></saleBusIncomeWin>
    </view>
</template>

<script>
import saleTitle from "./SaleTitle.vue";
import saleHead from "./SaleHead.vue";
import saleMenu from "./SaleMenu.vue";
import salesComp from "./SalesComp.vue";
import saleRanking from "./SalesRanking.vue";
import saleFlows from "./saleFlows.vue";
import saleBusIncomeWin from "../../comm/PopSaleBusIncomeWin.vue";
import saleQuarterWin from "../../comm/PopSaleQuarterWin.vue";
import saleMoothWin from "../../comm/PopSaleMoothWin.vue";

import https from "../../../js/https/Https.js";
import staticConst from "../../../js/const/StaticConst.js";
import lang from "../../../js/conf/Lang.js";
export default {
    components: {
        saleTitle,
        saleMenu,
        saleHead,
        salesComp,
        saleRanking,
        saleFlows,
        saleBusIncomeWin,
        saleMoothWin,
        saleQuarterWin,
    },
    data() {
        return {
            reqShopCount: {
                shopid: "", //非必须 门店筛选 默认所有
                group: "", //非必须 报表类型 1 日报 2月报 3季报  5 自定义时间的日报 默认按日统计
                stime: "", //非必须 自定义时间统计 开始时间 格式"2020-01-28"
                dtime: "", //分页参数  默认为1 该参数仅为日报时候使用， 其他条件不参与分页
                page: 1, //分页参数  默认为1 该参数仅为日报时候使用， 其他条件不参与分页
                is_yy: 2, //预约板块1是2 否 默认否
                uid_cp: "", //成员id
            },

            reqSellList: {
                shopid: "", //非必须 门店筛选 默认所有
                time: "", //非必须 时间筛选  1日 2月 3季 4年 5 自定义  默认为1 此项可不填
                stime: "", //自定义时间 指定查询某一天 格式y-m-d
                pfrom: 0, //非必须 分页参数 - 页面
                pnum: staticConst.PAGE_SIZE, //非必须 分页参数 - 页面显示数量
                is_yy: 2, //预约板块1是2 否 默认否
                uid_cp: "", //成员id
            },
            is_soll: true,
            saleFlowsList: [], //存储流水,
        };
    },
    methods: {
        initData() {
            this.loadReportShopConut();
        },
        // 日数据弹框滚动加载
        loadFun() {
            if (this.is_soll) return;
            this.reqSellList.pfrom++;
            this.loadSaleData();
        },
        loadReportShopConut() {
            let that = this;
            let param = this.reqShopCount;
            https.getReportShopConut(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    if (data.count_all) {
                        let hData = {};
                        hData.money = data.count_all.amount; //今日销售额
                        hData.reMoney = data.count_all.amount_th; //今日退货额
                        hData.grossProfit = data.count_all.profit; //毛利
                        hData.salesVolume = data.count_all.number; //销量
                        hData.oddNumber = data.count_all.num_od; //订单数量
                        that.$refs.saleHead.setData(hData, that.reqShopCount);
                    }
                    that.$refs.saleMenu.initData(data);
                    if (data.top) {
                        //排行
                        that.$refs.saleRanking.initData(
                            that.reqShopCount.group,
                            data.top
                        );
                    }
                    if (data.count_ls) {
                        console.log(that.reqShopCount.group,'that.reqShopCount.group');
                        //流水
                        if (
                            that.reqShopCount.group != 2 &&
                            that.reqShopCount.group != 3 &&
                            that.reqShopCount.group != 5
                        ) {
                            that.saleFlowsList = that.saleFlowsList.concat(
                                data.count_ls
                            );
                            that.reqShopCount.page++;
                        } else {
                            that.saleFlowsList = data.count_ls;
                        }
                        that.$refs.salesComp.init(that.saleFlowsList);
                        that.$refs.saleFlows.initData(
                            that.reqShopCount.group,
                            that.saleFlowsList
                        );
                    }
                }
            }
        },

        loadData(data) {
            this.saleFlowsList = [];
            this.reqShopCount = {
                shopid: data.curShop.id,
                group: data.curTime.id, //非必须 报表类型 1 日报 2月报 3季报  5 自定义时间的日报 默认按日统计
                stime: data.curTime.st, //非必须 自定义时间统计 开始时间 格式"2020-01-28"
                dtime: data.curTime.et, //分页参数  默认为1 该参数仅为日报时候使用， 其他条件不参与分页
                page: 1, //分页参数  默认为1 该参数仅为日报时候使用， 其他条件不参与分页
                is_yy: 2, //预约板块1是2 否 默认否
                uid_cp: data.curUcp.id, //成员id
            };
            this.loadReportShopConut();
        },

        loadFlowsList() {
            if (
                this.reqShopCount.group == 2 ||
                this.reqShopCount.group == 3 ||
                this.reqShopCount.group == 5
            )
                return;
            this.loadReportShopConut();
        },

        /*****日销售数据列表*****/
        async loadSaleData() {
            this.is_soll = true;
            let that = this;
            this.reqSellList.pfrom =
                this.reqSellList.pfrom * this.reqSellList.pnum;
            let param = this.reqSellList;
            await https.getReportShopSellList(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (data.data.data) {
                        that.ls_data = that.ls_data.concat(
                            data.data.data[0].order_ls
                        );
                        let obj = {};
                        obj.data = data.data;
                        obj.data.data[0].order_ls = that.ls_data;
                        obj.title = lang.getMD(param.stime);
                        that.$refs.saleBusIncomeWin.show("show", obj);
                        setTimeout(() => {
                            that.is_soll = false;
                        }, 300);
                    } else {
                        that.is_soll = true;
                        that.$msg({ title: "暂无数据" });
                    }
                }
            }
        },

        async saleBunsinessIncome(item, group) {
            if (group == 2 || group == 3) {
                //月.//季度
                this.loadQuatAndMoonFlows(item, group);
            } //其他
            else {
                this.is_soll = false;
                this.ls_data = [];
                this.reqSellList = {
                    shopid: this.reqShopCount.shopid,
                    time: 1, //非必须 时间筛选  1日 2月 3季 4年 5 自定义  默认为1 此项可不填
                    stime: item.timestr, //自定义时间 指定查询某一天 格式y-m-d
                    pfrom: 0, //非必须 分页参数 - 页面
                    pnum: staticConst.PAGE_SIZE, //非必须 分页参数 - 页面显示数量
                    is_yy: 2, //预约板块1是2 否 默认否
                    uid_cp: this.reqShopCount.uid_cp, //成员id
                };
                await this.loadSaleData();
            }
        },

        async loadQuatAndMoonFlows(item, group) {
            let that = this;
            let param = {};
            param.shopid = this.reqShopCount.shopid;
            param.group = group;
            if (group == 3) {
                param.group = 2;
            } else if (group == 2) {
                param.group = 1;
            }
            param.stime = item.timestr;
            param.dtime = "";
            param.page = 1;
            param.is_yy = 2;
            param.uid_cp = this.reqShopCount.uid_cp;
            await https.getReportShopConut(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    let hData = {};
                    if (data.count_all) {
                        hData.amount = data.count_all.amount; //今日销售额
                        hData.amount_th = data.count_all.amount_th; //今日退货额
                        hData.profit = data.count_all.profit; //毛利
                        hData.number = data.count_all.number; //销量
                        hData.num_od = data.count_all.num_od; //订单数量
                    }

                    let folwList = [];

                    if (data.count_ls) {
                        //流水
                        folwList = data.count_ls;
                    }

                    let obj = {};
                    obj.title = item.time;
                    obj.count = hData;
                    obj.list = folwList;
                    obj.group = param.group;
                    if (group == 2) {
                        that.$refs.saleMoothWin.show("show", obj);
                    } else if (group == 3) {
                        that.$refs.saleQuarterWin.show("show", obj);
                    }
                }
            }
        },
    },
};
</script>

<style lang="scss">
.context {
    margin-left: 0;
    position: fixed;
    width: 100%;//calc(100% - 72px);
    height: 100vh;
    color: #333;
    background-color: #fff;
    .box {
        height: 100%;
		width: 100%;
		position: relative;
        .s-c-t-f-box {
            display: flex;
            height: calc(100vh - 275px);
            position: relative;
            font-size: 14px;
            color: #333;
        }
    }
}
</style>
